package com.bainuo.doctor.common.util;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;


@SuppressWarnings("deprecation")
public class DisplayUtil {

	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public static int dipToPx(Context c, float dipValue) {
		DisplayMetrics metrics = c.getResources().getDisplayMetrics();
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
	}

	public static int spToPx(Context context, float spValue) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, metrics);
	}
	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int pxTodip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
	/*
	 *  获取屏幕高度
	 */
	public static int getDisplayHeight(Activity c) {
		DisplayMetrics dm = new DisplayMetrics();

		c.getWindowManager().getDefaultDisplay().getMetrics(dm);
		// int width = dm.widthPixels;
		return dm.heightPixels;
	}

	/*
	 *  获取屏幕宽度
	 */
	public static int getDisplayWidth(Activity c) {
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		c.getWindowManager().getDefaultDisplay().getMetrics(dm);
		// int width = dm.widthPixels;
		return dm.widthPixels;
	}

}
